<?php
/**
*
* @author Blackpawn blackpawn666@hotmail.com - http://blackpawnsarchives.net
* @package phpbb3 :: Game Console Mod
* @version $Id: index.php 1.0.2 12-10-2009 Blackpawn $
* @copyright (c) 2004-2009 Blackpawns Archives
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/


define('GC_MOD', true);
define('IN_PHPBB', true);

$phpbb_root_path =  '../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/mods/game_console/gc_common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

$user->add_lang('mods/game_console');

if ($user->data['user_id'] == ANONYMOUS)
{
	login_box('', $user->lang['LOGIN']);
}


if ($user->data['is_bot'])
{
	redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
}

if (!$auth->acl_get('u_gc') && !$auth->acl_get('a_'))
{
	trigger_error($user->lang['UNAUTHORIZED_ACCESS'])	;
}

$cmd = request_var('cmd','');
$game_id = request_var('gid', 0);
$mode = request_var('mode', 0);
$start = request_var('start', 0);
$games_per_page = $game_config['games_per_page'];
$pagination_url = append_sid("index.$phpEx");
$error_msgs = null;

$game_list = gc_fetch_games();

switch($cmd)
{
	case $user->lang['CREATE_ACCOUNT_BUTTON']:
		$error_msgs = 'Create';
		$mode = 1;
	break;
	
	case $user->lang['DELETE_ACCOUNT_BUTTON']:
		$error_msgs = 'Delete';
		$mode = 2;
	break;
	
	case $user->lang['PLAY_BUTTON']:
		$error_msgs = 'Play';
		$mode = 3;
	break;
	
	case $user->lang['ABOUT_BUTTON']:
		$mode = 4;
	break;	
}

switch($mode)
{

	case 1: // Create Account
		$game_path = $game_list[$game_id]['path'];
		redirect(append_sid("{$phpbb_root_path}games/$game_path/index.$phpEx?gid=$game_id"));
	break;
		
	case 2:// Delete Account
		$game_path = $game_list[$game_id]['path'];
		redirect(append_sid("{$phpbb_root_path}games/$game_path/index.$phpEx?cmd=delete_account&gid=$game_id"));
	break;
	
	case 3:// Play the game
		$game_path = $game_list[$game_id]['path'];
		redirect(append_sid("{$phpbb_root_path}games/$game_path/index.$phpEx?gid=$game_id"));
	break;

	case 4:// See info on the game
		$arrow_img = '<img src="images/arrow.png" width="9" height="9" border="0">';
	
		// Display an error messages to the user
		$error_msgs = error_msgs($error_msgs);

		$custom_css = load_custom_css();
	
		$template->assign_vars(array(
			'CSS'					=>	$custom_css,
			'ERROR_MSGS'			=>	$error_msgs,
			'GAME_TITLE'			=>	$game_list[$game_id]['name'],
			'GAME_VERSION'			=>	sprintf($user->lang['GAME_VERSION'],$game_list[$game_id]['ver_major'],$game_list[$game_id]['ver_minor'],$game_list[$game_id]['ver_build']),
			'VERSION_MAJOR'			=>	$game_list[$game_id]['ver_major'],
			'VERSION_MINOR'			=>	$game_list[$game_id]['ver_minor'],
			'VERSION_BUILD'			=>	$game_list[$game_id]['ver_build'],
			'AUTHOR'				=>	$game_list[$game_id]['author'],
			'EMAIL'					=>	$game_list[$game_id]['email'],
			'WEBSITE'				=>	$game_list[$game_id]['website'],
			'CREDITS'				=>	str_replace('\n','<br>',$game_list[$game_id]['credits']),
			'HISTORY'				=>	str_replace('->',$arrow_img,$game_list[$game_id]['history']),
			'LICENSE'			=>	$game_list[$game_id]['license'],
			'COPYRIGHTS'		=>	$game_list[$game_id]['copyrights'],
					
		));
		
		// Output page
		page_header($user->lang['CONSOLE_TITLE']);

		$template->set_filenames(array(
			'body' => 'mods/game_console/games_about_body.html')
		);
	
		break;

	Default:


		$sql = 'SELECT id, game_name, game_descript, game_ver_major, game_ver_minor, game_ver_build, game_round, 
		game_round_length, game_ticks_left, game_time_mgt, game_members, game_member_slots, game_path, 
		game_bulletin, game_bulletin_show, game_online, game_open, game_license, game_copyrights
		FROM ' . GC_GAMES_TABLE . '
		ORDER BY game_name ASC';
		$result = $db->sql_query_limit($sql, $games_per_page, $start);
		
		$games=0;
		
		while ($row = $db->sql_fetchrow($result))
		{

						
			switch($row['game_online'])
			{
				Case 1:
					$game_status_img = 'images/1.png';
					$game_status_msg = $user->lang['GAME_STATUS_MSG_1'];
					$game_is_online = True;
  				break;
				Case 2:
					$game_status_img = 'images/2.png';
					$game_status_msg = $user->lang['GAME_STATUS_MSG_2'];
					$game_is_online = True;
				break;
				Default:
					$game_status_img = 'images/0.png';
					$game_status_msg = $user->lang['GAME_STATUS_MSG_DEFAULT'];
					$game_is_online = False;
				break;
			}

			switch($row['game_open'])
			{
				Case 1:
					$game_signups_img = 'images/2.png';
					$game_signups_msg = $user->lang['GAME_SIGNUP_MSG_1'];
					$game_signups_open = True;
				break;
				Default:
					$game_signups_img = 'images/0.png';
					$game_signups_msg = $user->lang['GAME_SIGNUP_MSG_DEFAULT'];
					$game_signups_open = False;
				break;
			}

			if($row['game_member_slots'] < 1)
			{
				$free_slots = '&#8734;';
				$space_full = False;
				
			}else{
				$available_slots = ($row['game_member_slots'] - $row['game_members']);
				$free_slots = number_format($available_slots); 
				if($free_slots == 0)
				{
					$space_full = True;
				}else{
					$space_full = False;
				}
				
			}
			
			$account_status = null;
			$account_status = gc_check_account_status($user->data['user_id'],$row['id']);
  
			if(isset($row['game_img']))
			{
				$game_img = $row['game_img'];
			}else{
				$game_img = 'images/none.png';	
			}

			$template->assign_block_vars('gamerow', array(
				'S_GAME_IS_ONLINE'		=>	$game_is_online,
			 	'S_GAME_IS_OPEN'		=>	$game_signups_open,
			 	'S_GAME_IS_FULL'		=>	$space_full,
			 	'S_GAME_ACC_STATUS'		=>	$account_status,
			 	'S_GAME_BULLETIN_SHOW'	=>	$row['game_bulletin_show'],
			 	
				'GAME_BULLETIN'			=>	$row['game_bulletin'],			
			 	'GAME_STATUS_IMG'		=>	$game_status_img,
				'GAME_STATUS_MSG'		=>	$game_status_msg,
				'GAME_SIGNUPS_IMG'		=>	$game_signups_img,
				'GAME_SIGNUPS_MSG'		=>	$game_signups_msg,
				'GAME_ID'				=>	$row['id'],
				'GAME_NAME' 			=>	$row['game_name'],
				'GAME_DESCRIPT' 		=>	$row['game_descript'],
				'GAME_VER_MAJOR' 		=>	$row['game_ver_major'],
				'GAME_VER_MINOR' 		=>	$row['game_ver_minor'],
				'GAME_VER_BUILD' 		=>	$row['game_ver_build'],
				'GAME_ROUND' 			=>	$row['game_round'],
				'TICKS_LEFT' 			=>	number_format($row['game_ticks_left']),
				'GAME_MEMBERS'			=>	$row['game_members'],
				'GAME_FREE_SLOTS'		=>	$free_slots,
				'GAME_IMG'				=>	strtolower($row['game_path']).'/images/img.png',
			));	
	
			++$games;	
		}	
		$db->sql_freeresult($result);
		
		if(count($games) > 0)
		{
			$games_listed = true;
		}else{
			$games_listed = false;
		}

		// Display an error messages to the user
		$error_msgs = error_msgs($error_msgs);

		$custom_css = load_custom_css();
		$total_games = gc_fetch_game_count();

		// Assign index specific vars
		$template->assign_vars(array(
			'CSS'					=>	$custom_css,
			'ERROR_MSGS'			=>	$error_msgs,
			'PAGINATION'			=>	generate_pagination($pagination_url, $total_games, $games_per_page, $start),
			'PAGE_NUMBER'			=>	on_page($total_games, $games_per_page, $start),
			'TOTAL_GAMES'			=>	($total_games == 1) ? $user->lang['LIST_GAME'] : sprintf($user->lang['LIST_GAMES'], number_format($total_games)),
			'GAME_COUNT'			=>	number_format($total_games),
			'S_GAME_COUNT_LISTED'	=>	$games_listed,

		));
		// Output page
		page_header($user->lang['CONSOLE_TITLE']);

		$template->set_filenames(array(
			'body' => 'mods/game_console/index_body.html')
		);
		break;
}
page_footer();
?>